package com.desire;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author desire
 */
public class CommonLogConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        // 配置连接信息  类似于 MySQL连接信息
        // 设置用户名 密码
        factory.setUsername("admin");
        factory.setPassword("123");
        // 设置mq服务器地址 默认是 localhost
        factory.setHost("116.63.151.50");
        // 设置高级消息队列amqp 端口号
        factory.setPort(5672);
        // 设置虚拟主机，默认是 /
        factory.setVirtualHost("erp");

        // 获取连接
        Connection connection = factory.newConnection();
        // 在连接上创建信道
        Channel channel = connection.createChannel();
        // 声明队列
        String commonlog = "commonlog";
        channel.queueDeclare(commonlog, false, false, false, null);
        System.out.println("commonlog");
        // 从队列中获取消息 并消费消息
        channel.basicConsume(commonlog, true, (consumerTag, message) -> {
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            System.out.println("接收到消息：" + msg);
        }, (CancelCallback) null);
    }
}
